package module;

import Client.ClientInitClose;
import Client.ClientScanner;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class StudentManage {

    private static List<Student> students = new ArrayList<>();
    private ClientInitClose client;

    public void addStudent() throws StudentIdException, AgeException, IOException, ClassNotFoundException {
        //添加学生信息
        System.out.println("请输入姓名：");
        String name = ClientScanner.getScanner().next();
        System.out.println("请输入学号：");
        int num = ClientScanner.getScanner().nextInt();
        System.out.println("请输入年龄：");
        int age = ClientScanner.getScanner().nextInt();
        if(num < 0){
            throw new StudentIdException("请输入合理的学号");
        }else if(age < 0) {
            throw new AgeException("请输入大于0的年龄：");
        }else{
            client.getOut().writeObject("add"+";"+name+";"+ num +";"+age);
            if("success".equals(client.getIn().readObject())){
                System.out.println("添加成功！");
            }else{
                System.out.println("添加失败！");
            }
        }
    }

    public void deleteStudents() throws StudentIdException, IOException, ClassNotFoundException {
        System.out.println("请输入删除学生的学号：");
        int num = ClientScanner.getScanner().nextInt();
        if(num < 0){
            throw new StudentIdException("请输入合理的学号");
        }else {
            client.getOut().writeObject("delete"+";"+ num);
            if("success".equals(client.getIn().readObject())){
                System.out.println("删除成功！");
            }else{
                System.out.println("删除失败，查无此人");
            }
        }
    }

    public void findStudents() throws StudentIdException, IOException, ClassNotFoundException {
        System.out.println("请入要查找学生的学号：");
        int num = ClientScanner.getScanner().nextInt();
        if(num < 0){
            throw new StudentIdException("请输入合理的学号");
        }else {
            client.getOut().writeObject("find"+";"+ num);
            while (true) {
                String str = (String) client.getIn().readObject();
                if ("success".equals(str)){
                    Student students = (Student) client.getIn().readObject();
                    System.out.println("学号为"+num+"信息如下："+students);
                } else if ("end".equals(str)) {
                    return;
                } else {
                    System.out.println("删除失败，查无人");
                    return;
                }
            }
        }
    }

    public void modifyStudents() throws StudentIdException, AgeException, IOException, ClassNotFoundException {
        System.out.println("请输入需要修改学生的学号：");
        int num = ClientScanner.getScanner().nextInt();
        System.out.println("请输入要修改学生的新信息：");
        System.out.println("请输入姓名");
        String  name = ClientScanner.getScanner().next();
        System.out.println("清输入学号");
        int newnum = ClientScanner.getScanner().nextInt();
        System.out.println("请输入年龄");
        int age = ClientScanner.getScanner().nextInt();
        if(num < 0){
            throw new StudentIdException("请输入合理的学号");
        }else if(newnum < 0){
            throw new StudentIdException("请输入合理的新学号");
        }else if(age < 0){
            throw new AgeException("请输入合理的学号");
        }else {
            client.getOut().writeObject("modify"+";"+ num +";"+name+";"+ newnum +";"+ age);
            if("success".equals(client.getIn().readObject())){
                System.out.println("修改成功！");
            }else {
                System.out.println("修改失败，查无此人");
            }
        }
    }

    public void forStudent() throws IOException, ClassNotFoundException {
        client.getOut().writeObject("for"+";");
        students =(List<Student>) client.getIn().readObject();
        for(Student student:students){
            System.out.println(student.toString());
        }
    }

    public void addaccount() throws IOException, ClassNotFoundException {
        System.out.println("请输入该学生学号：");
        int num = ClientScanner.getScanner().nextInt();
        System.out.println("请输入账号：");
        String account = ClientScanner.getScanner().next();
        System.out.println("请输入密码：");
        String password = ClientScanner.getScanner().next();
        client.getOut().writeObject("addaccount"+";"+account+";"+password+";"+num);
        if("success".equals(client.getIn().readObject())){
            System.out.println("修改成功！");
        }else {
            System.out.println("修改失败，查无此人");
        }
    }

    public void studentManage(ClientInitClose client) {

        this.client = client;

        while (true) {
            System.out.println("------学生信息管理系统------");
            System.out.println("  1、增加学生信息");
            System.out.println("  2、删除学生信息");
            System.out.println("  3、修改学生信息");
            System.out.println("  4、查找学生信息");
            System.out.println("  5、遍历学生信息");
            System.out.println("  6、添加学生账号");
            System.out.println("  0、退出学生信息");
            int choose = ClientScanner.getScanner().nextInt();
            try {
                switch (choose){
                    case(1):
                            addStudent();
                        break;
                    case(2):
                            deleteStudents();
                        break;
                    case(3):
                            modifyStudents();
                        break;
                    case(4):
                            findStudents();
                        break;
                    case (5):
                            forStudent();
                        break;
                    case 6:
                        addaccount();
                        break;
                    case 0:
                        client.getOut().writeObject("end");
                        return;
                    default:
                        System.out.println("请重新输入0-5选择对应功能！");
                }
            } catch (StudentIdException | AgeException | IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }

        }
    }
}
